<template>
  <div class="update_auth_page">
    <CommonHeader title="解绑谷歌验证" />
    <div class="update_auth_content page_no_tab_bar">
      <!-- 旧邮箱验证码 -->
      <div class="input_box">
        <div class="input_label">将发送验证码到您的 {{ user.userInfo.email }} 邮箱上</div>
        <div class="input input_psw">
          <input placeholder="请输入邮箱验证码" v-model="ruleForm.emailCode" />
          <div class="is_open add_text" @click="getCode">
          {{ curTime ? `${curTime}s` : "发送验证码" }}
        </div>
        </div>
        <div class="input_rule">{{ rules.emailCode.rule }}</div>
      </div>
      <!-- 谷歌验证码 -->
      <div class="input_box">
        <div class="input_label">谷歌验证码<span class="cut_text">*</span></div>
        <div class="input input_psw">
          <input placeholder="请输入谷歌验证码" v-model="ruleForm.googleCode" />
        </div>
        <div class="input_rule">{{ rules.googleCode.rule }}</div>
      </div>
      <!-- 操作按钮 -->
      <div class="operate_btn" @click="toSubmit">提交</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import CommonHeader from "@/components/CommonHeader.vue";
import { useRouter } from "vue-router";
import { useUserInfo } from "@/stores/userInfo";
import { ElMessage } from "element-plus";
import { getProfile, sendEmail } from "@/api/user";
import { googleUnBind } from "@/api/system";

// 获取路由对象
const router = useRouter()
// 获取用户信息
const user = useUserInfo()
// 倒计时变量
const curTime = ref(0); // 倒计时
const isFlag = ref(true); // 控制频繁发送
// 表单数据
const ruleForm: any = ref({
  emailCode: "",
  googleCode: "",
  googleToken: "",
});
// 表单验证
const rules: any = reactive({
  emailCode: {
    msg: "请输入邮箱验证码",
    rule: "",
  },
  googleCode: {
    msg: "请输入谷歌验证码",
    rule: "",
  },
});

// 获取验证码
const getCode = () => {
  if (curTime.value) return;
  if (!isFlag.value) return ElMessage.error("请勿频繁请求！");
  isFlag.value = false;

  // 请求邮箱验证码
  sendEmail({ email: user.userInfo.email, useTo: "8" }).then((res: any) => {
    downCount();
    ElMessage.success("获取验证码成功，请注意您的邮箱！");
  });
};

// 提交
const toSubmit = () => {
  if (validateForm()) {
    // 组装数据
    googleUnBind(ruleForm.value).then((res: any) => {
      ElMessage.success("解绑谷歌成功！");
      getProfile().then((res: any) => {
        user.setUserInfo(res.data);
        router.replace("/profile/security");
      });
    });
  }
}

// 表单验证
const validateForm = () => {
  for (let k in rules) {
    // 验证是否为空
    if (ruleForm.value[k].trim() == "") {
      rules[k].rule = rules[k].msg;
      return false;
    }
  }
  for (let k in rules) {
    rules[k].rule = "";
  }
  return true;
};

// 验证码倒计时
const downCount = () => {
  let num = 60;
  curTime.value = num;
  let timer = setInterval(() => {
    if (num < 1) {
      clearInterval(timer);
      isFlag.value = true;
    } else {
      num--;
      curTime.value = num;
    }
  }, 1000);
};

</script>

<style lang="scss" scoped>
.update_auth_page {
  .update_auth_content {
    padding-top: 10px;
    .operate_btn {
      margin-top: 50px;
    }
  }
}
</style>
